const Service = require('egg').Service;
const child_process = require("child_process")
const fs = require("fs")
var moment = require('moment');

class AdbUtilService extends Service {
    //  复制链接
    copyUrl(video_url) {
        const adbProcess = child_process.spawn("adb", [
            "shell",
            "am ",
            "broadcast",
            "-a",
            "clipper.set",
            "-e",
            "text",
            video_url
        ])
        return new Promise((resolve, reject) => {
            adbProcess.stderr.on("data", function (data) {
                reject(data)
            })
            adbProcess.on("error", err => {
                reject(err)
            })
            adbProcess.on("exit", code => {
                if (code === 0) {
                    resolve()
                }
            })
        })
    }
    openSoft(item) {
        var openPackage
        if (item.media_name === "快手") {
            openPackage = "com.smile.gifmaker/com.yxcorp.gifshow.HomeActivity"
        } else if (item.media_name === "抖音") {
            openPackage = "com.ss.android.ugc.aweme/com.ss.android.ugc.aweme.splash.SplashActivity"
        } else if (item.media_name === "火山") {
            openPackage = "com.ss.android.ugc.live/com.ss.android.ugc.live.detail.DetailActivity"
        }
        const adbProcess = child_process.spawn("adb", [
            "shell",
            "am ",
            "start",
            openPackage
        ])
        return new Promise((resolve, reject) => {
            adbProcess.stderr.on("data", function (data) {
                reject(data)
            })
            adbProcess.on("error", err => {
                reject(err)
            })
            adbProcess.on("exit", code => {
                if (code === 0) {
                    resolve()
                }
            })
        })
    }
    // 点击
    tap(x, y) {
        const adbProcess = child_process.spawn("adb", [
            "shell",
            "input",
            "tap",
            x,
            y
        ])
        return new Promise((resolve, reject) => {
            adbProcess.on("error", err => {
                reject(err)
            })
            adbProcess.stderr.on("data", function (data) {
                reject(data)
            })
            adbProcess.on("exit", code => {
                if (code === 0) {
                    resolve()
                }
            })
        })
    }

    screenshot(item) {
        if (item) {
            item.fileName = item.id + '.png'
        } else {
            item = {
                fileName: 'screenshot.png'
            }
        }
        const adbProcess = child_process.spawn("adb", [
            "shell",
            "screencap",
            "-p",
            "/sdcard/" + item.fileName
        ])
        return new Promise((resolve, reject) => {
            adbProcess.stderr.on("data", function (data) {
                reject(data)
            })
            adbProcess.on("error", err => {
                reject(err)
            })
            adbProcess.on("exit", code => {
                if (code === 0) {
                    // resolve()
                    this.pullFile(item).then(resolve, reject)
                }
            })
        })
    }
    goBackHOme() {
        const adbProcess = child_process.spawn("adb", [
            "shell",
            "input",
            "keyevent",
            "KEYCODE_HOME"
        ])
        return new Promise((resolve, reject) => {
            adbProcess.stderr.on("data", function (data) {
                reject(data)
            })
            adbProcess.on("error", err => {
                reject(err)
            })
            adbProcess.on("exit", code => {
                if (code === 0) {
                    resolve()
                }
            })
        })
    }
    pullFile(item) {
        var _today = moment();
        var date = _today.format('YYYY-MM-DD'); /*现在的时间*/
        // fs.mkdirSync("./imagesBig/" + date);
        var distPath = "./imagesBig/" + date + '/' + item.fileName
        console.log(distPath)
        const adbProcess = child_process.spawn("adb", [
            "pull",
            "/sdcard/" + item.fileName,
            distPath
        ])
        return new Promise((resolve, reject) => {
            adbProcess.stderr.on("data", function (data) {
                reject(data)
            })
            adbProcess.on("error", err => {
                reject(err)
            })
            adbProcess.on("exit", code => {
                if (code === 0) {
                    resolve()
                }
            })
        })
    }


}

module.exports = AdbUtilService;